package cn.apelx.order.domain;

import lombok.Getter;
import org.springframework.data.domain.Persistable;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.UUID;

/**
 * 订单
 *
 * @author apelx
 * @since 2022-05-01
 */
@Getter
@Entity
@Table(name = "orders")
public class Orders implements Persistable<String>, Serializable {

    @Id
    private String id;

    private String name;

    private BigDecimal price;

    protected Orders() {

    }

    public Orders(String name, BigDecimal price) {
        this.name = name;
        this.price = price;
    }

    @PrePersist
    public void prePersist() {
        if (isNew()) {
            this.id = UUID.randomUUID().toString().toLowerCase().replaceAll("-", "");
        }
    }


    @Override
    public String getId() {
        return id;
    }

    @Override
    public boolean isNew() {
        return null == getId();
    }
}
